# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=gdb
pkgver=9.2
pkgrel=3
_gcc_ver=9.3.0
pkgdesc="GNU Debugger (MSYS2 version)"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/gdb/"
groups=('base-devel')
depends=("libiconv" "zlib" "expat" "python" "libexpat" "libreadline" "mpfr")
#checkdepends=('dejagnu' 'bc')
makedepends=("libiconv-devel" "zlib-devel" "ncurses-devel" "liblzma-devel" "mpfr-devel" "libexpat-devel" "libreadline-devel" "python-devel")
options=('staticlibs' '!distcc' '!ccache')
source=("https://ftp.gnu.org/gnu/gdb/gdb-${pkgver}.tar.xz"{,.sig}
        'gdbinit'
        0001-Teach-gdb-how-to-unwind-cygwin-_sigbe-and-sigdelayed.patch
        0002-7.8-windows-nat-cygwin.patch
        0003-Better-handling-for-realpath-failures-in-windows_mak.patch
        0004-7.8-symtab-cygwin.patch
        0005-Enable-CLI-styling-by-default-on-Cygwin.patch
        0006-Move-thread_local_segv_handler-definition.patch
        'gdb-9.1-msysize.patch')
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('360cd7ae79b776988e89d8f9a01c985d0b1fa21c767a4295e5f88cb49175c555'
            'SKIP'
            '2bbe7eddb1828c394d0ff99777058df79b1a596172603bb0e30d983fc1ea8785'
            '2bdd02d6dc46d1186a3fa4485922d277686ca9ff827dde6b9fa1a90e9345c953'
            '295fa28ad89e7e8bde2dd846644e3a9dc92485c4b87c6d17244fb02b19e09ccb'
            '91994d62047bb76688a785c9a911f8deda6e7b7aa258aade0a6b29904a9e9b58'
            '6d368418cb7654d7054996b4bc3d7a24ef440028fd1007b204b1429aa7e97b11'
            '2156ad9e4b0ab528ec7ed4edf36ac2cbb6ef3092c71964edba16560d62a79878'
            'aac0a21993be88a5414e231ead1a81b318e821869bccb73dd863dc5aab357769'
            '85d6e7f728f103b6e87ad7fc240ed5e31469bedff7a05e361462b433344b1338')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/0001-Teach-gdb-how-to-unwind-cygwin-_sigbe-and-sigdelayed.patch
  patch -p1 -i ${srcdir}/0002-7.8-windows-nat-cygwin.patch
  patch -p1 -i ${srcdir}/0003-Better-handling-for-realpath-failures-in-windows_mak.patch
  patch -p1 -i ${srcdir}/0004-7.8-symtab-cygwin.patch
  patch -p1 -i ${srcdir}/0005-Enable-CLI-styling-by-default-on-Cygwin.patch
  patch -p1 -i ${srcdir}/0006-Move-thread_local_segv_handler-definition.patch
  patch -p1 -i ${srcdir}/gdb-9.1-msysize.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  [[ -d "${srcdir}/build-${CHOST}" ]] && rm -rf "${srcdir}/build-${CHOST}"
  mkdir -p "${srcdir}/build-${CHOST}" && pushd "${srcdir}/build-${CHOST}"

  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --prefix=/usr \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-staticlib \
    --with-system-gdbinit=/etc/gdbinit \
    --with-python=/usr/bin/python3 \
    --without-tcl \
    --without-tk \
    --without-guile \
    --with-expat \
    --with-lzma \
    --with-mpfr \
    --without-libexpat-prefix \
    --with-system-readline \
    --with-libiconv-prefix=/usr \
    --with-system-zlib \
    --enable-tui
  make
}

check() {
  cd ${srcdir}/build-${CHOST}
  make check
}

package() {
  cd ${srcdir}/build-${CHOST}
  make DESTDIR=${pkgdir} install

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${_gcc_ver}'|g' ${pkgdir}/etc/gdbinit

  # these are shipped by binutils
  rm -fr ${pkgdir}/usr/{include,lib}/ ${pkgdir}/usr/share/locale/
  rm -f ${pkgdir}/usr/share/info/{bfd,configure,standards}.info
}
